<template>
  <div class="visitor-container">
    <div class="top-item-group">
      <el-select v-model="serviceValue" clearable placeholder="客户等级">
        <el-option
          v-for="item in serviceOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <el-select v-model="sourceValue" clearable placeholder="全部来源">
        <el-option
          v-for="item in sourceOptions"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        ></el-option>
      </el-select>
      <!--      <el-date-picker-->
      <!--        v-model="value1"-->
      <!--        type="date"-->
      <!--        placeholder="开始日期">-->
      <!--      </el-date-picker>-->
      <!--      <el-date-picker-->
      <!--        v-model="value2"-->
      <!--        type="date"-->
      <!--        placeholder="结束日期">-->
      <!--      </el-date-picker>-->
      <el-date-picker
        style="width:250px"
        v-model="value1"
        type="daterange"
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
      ></el-date-picker>
      <el-input placeholder="请输入关键词搜索" v-model="searchKeyword" clearable></el-input>
      <el-button type="primary" @click="handleSearch">搜索</el-button>
      <div style="float:right">
        <el-button @click="createDialogVisible = true" type="primary">添加用户</el-button>
        <el-button @click="handlePartialDelete" type="primary">批量操作</el-button>
        <el-button>导出数据</el-button>
      </div>
    </div>
    <div class="middle-table-gorup">
      <el-table
        :header-cell-style="{'background-color':'#e6f1ff','height':'62px'}"
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        v-loading="isLoading"
        @selection-change="handleSelectionChange"
        :cell-style="cellStyle"
      >
        <el-table-column type="selection" width="55" align="center"></el-table-column>
        <el-table-column prop="customerName" label="客户名称" align="center"></el-table-column>
        <el-table-column prop="realName" label="真实姓名" align="center"></el-table-column>
        <el-table-column prop="phone" label="电话" align="center"></el-table-column>
        <el-table-column prop="email" label="邮箱" align="center"></el-table-column>
        <el-table-column prop="level" label="客户等级" align="center"></el-table-column>
        <el-table-column prop="channel" label="客户来源" align="center"></el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              @click.native.prevent="getDetail(scope.$index, tableData)"
              type="text"
              size="normal"
            >详情</el-button>
            <el-button
              @click.native.prevent="createOrder(scope.$index, tableData)"
              type="text"
              size="normal"
            >创建工单</el-button>
            <el-button
              @click.native.prevent="deleteCustomer(scope.$index, tableData)"
              type="text"
              size="normal"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="bottom-pagination-group">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 20, 30, 40]"
          :page-size="pageSize"
          layout="total, prev, pager, next,sizes, jumper"
          :total="totalData"
        ></el-pagination>
      </div>
    </div>

    <el-dialog title="新建工单" top="9vh" :visible.sync="addDialogVisible" width="33%">
      <div class="add-dialog-style">
        <el-form ref="addOrderForm" :model="addOrderForm" label-position="left" label-width="120px">
          <el-form-item :rules="{ required: true, message: '请选择工单分类'}" label="工单分类" prop="workType">
            <el-select style="width:100%;" v-model="addOrderForm.workType" placeholder="请选择">
              <el-option label="分类一" value="分类一"></el-option>
              <el-option label="分类二" value="分类二"></el-option>
              <el-option label="分类三" value="分类三"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item :rules="{ required: true, message: '请输入工单标题'}" label="工单标题" prop="title">
            <el-input style="width:100%;" v-model="addOrderForm.title" placeholder="请输入工单标题"></el-input>
          </el-form-item>
          <el-form-item
            :rules="{ required: true, message: '请输入工单描述'}"
            label="工单描述"
            prop="attachments"
          >
            <el-input
              type="textarea"
              :rows="6"
              v-model="addOrderForm.attachments"
              placeholder="请输入工单描述"
            ></el-input>
          </el-form-item>
          <el-upload
            class="upload-demo"
            action="https://jsonplaceholder.typicode.com/posts/"
            multiple
            :limit="3"
            :file-list="fileList"
          >
            <el-link
              type="primary"
              style="padding: 10px"
              :underline="false"
              @click="addFile"
            >添加附件（最多上传5个附件，单个文件最大50M)</el-link>
          </el-upload>
          <el-form-item label="抄送人" prop="cclist">
            <el-select style="width:100%;" v-model="addOrderForm.cclist" placeholder="请选择">
              <el-option
                v-for="csd in csData"
                :key="csd.customerServiceId"
                :label="csd.nickName"
                :value="csd.customerServiceId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="优先级" prop="priority">
            <el-radio-group v-model="addOrderForm.priority">
              <el-radio label="低"></el-radio>
              <el-radio label="中"></el-radio>
              <el-radio label="高"></el-radio>
              <el-radio label="紧急"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="工单状态" prop="state">
            <el-select style="width:100%;" v-model="addOrderForm.state" placeholder="请选择">
              <el-option label="未分配" value="未分配"></el-option>
              <el-option label="待处理" value="待处理"></el-option>
              <el-option label="处理中" value="处理中"></el-option>
              <el-option label="已解决" value="已解决"></el-option>
              <el-option label="已关闭" value="已关闭"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="受理客服组" prop="customerServiceGroupId">
            <el-select
              style="width:100%;"
              v-model="addOrderForm.customerServiceGroupId"
              placeholder="请选择"
            >
              <el-option
                v-for="gd in groupData"
                :key="gd.groupId"
                :label="gd.groupName"
                :value="gd.groupId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="受理客服">
            <el-select
              style="width:100%;"
              v-model="addOrderForm.customerServiceId"
              placeholder="请选择"
            >
              <el-option
                v-for="csd in csData"
                :key="csd.customerServiceId"
                :label="csd.nickName"
                :value="csd.customerServiceId"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="所属客户">
            <el-select style="width:100%;" v-model="addOrderForm.customerId" placeholder="请选择">
              <el-option :label="createCN" :value="createId"></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native.prevent="resetForm" type="text" size="normal">清空</el-button>
        <el-button @click="addDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleCreateOrder">创建工单</el-button>
      </div>
    </el-dialog>

    <el-dialog title="新建客户信息" top="9vh" :visible.sync="createDialogVisible" width="33%">
      <div class="add-dialog-style">
        <el-form
          ref="createOrderForm"
          :model="createOrderForm"
          :rules="rules"
          label-position="left"
          label-width="120px"
        >
          <el-form-item label="客户名称" prop="customerName">
            <el-input
              style="width:100%;"
              v-model="createOrderForm.customerName"
              placeholder="请输入客户名称"
            ></el-input>
          </el-form-item>
          <el-form-item label="真实姓名" prop="realName">
            <el-input
              style="width:100%;"
              v-model="createOrderForm.realName"
              placeholder="请输入客户真实姓名"
            ></el-input>
          </el-form-item>
          <el-form-item label="客户电话" prop="phone">
            <el-input style="width:100%;" v-model="createOrderForm.phone" placeholder="请输入客户客户电话"></el-input>
          </el-form-item>
          <el-form-item label="客户邮箱" prop="email">
            <el-input style="width:100%;" v-model="createOrderForm.email" placeholder="请输入客户客户邮箱"></el-input>
          </el-form-item>
          <el-form-item label="客户公司" prop="company">
            <el-input style="width:100%;" v-model="createOrderForm.company" placeholder="请输入客户客户公司"></el-input>
          </el-form-item>
          <el-form-item label="客户地址" prop="address">
            <el-input style="width:100%;" v-model="createOrderForm.address" placeholder="请输入客户客户地址"></el-input>
          </el-form-item>
          <el-form-item label="客户等级" prop="level">
            <el-select style="width:100%;" v-model="createOrderForm.level" placeholder="请选择客户等级">
              <el-option label="普通客户" value="普通客户"></el-option>
              <el-option label="VIP客户" value="VIP客户"></el-option>
              <el-option label="潜在客户" value="潜在客户"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="客户来源" prop="channel">
            <el-select style="width:100%;" v-model="createOrderForm.channel" placeholder="请选择客户来源">
              <el-option label="客服录入" value="客服录入"></el-option>
              <el-option label="网页介入" value="网页介入"></el-option>
            </el-select>
          </el-form-item>
          <!--          <el-form-item label="选择标签" prop="tagsId">-->
          <!--            <el-checkbox-group v-model="createOrderForm.tagsId">-->
          <!--              <el-checkbox label="潜在客户" ></el-checkbox>-->
          <!--              <el-checkbox label="上市公司" ></el-checkbox>-->
          <!--            </el-checkbox-group>-->
          <!--          </el-form-item>-->
          <el-form-item label="客户备注" prop="remark">
            <el-input
              type="textarea"
              :rows="6"
              v-model="createOrderForm.remark"
              placeholder="请输入客户备注"
            ></el-input>
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native.prevent="handleClean" type="text" size="normal">清空</el-button>
        <el-button @click="createDialogVisible = false">取消</el-button>
        <el-button type="primary" :loading="isLoading" @click="handleCreateCustomer">保存</el-button>
      </div>
    </el-dialog>

    <el-dialog title="删除确认" :visible.sync="deleteDialogVisible" top="34vh" width="26%">
      <div class="delete-dialog-style">客户信息删除后无法恢复，确定删除此客户信息吗？</div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="deleteDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="handleDelete">确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import api from "@/api/api";
export default {
  name: "Customer",
  components: {},
  props: {},
  data() {
    return {
      tableData: [],
      currentPage: 1,
      pageSize: 10,
      totalData: 0,
      type: [],
      isLoading: false,
      serviceOptions: [
        {
          value: "普通客户",
          label: "普通客户"
        },
        {
          value: "VIP客户",
          label: "VIP客户"
        },
        {
          value: "潜在客户",
          label: "潜在客户"
        }
      ],
      serviceValue: "",
      sourceOptions: [
        {
          value: "客服录入",
          label: "客服录入"
        },
        {
          value: "网页介入",
          label: "网页介入"
        }
      ],
      sourceValue: "",
      searchKeyword: "",
      value1: "",
      value2: "",
      multipleSelection: [],
      addDialogVisible: false,
      createDialogVisible: false,
      deleteDialogVisible: false,
      addOrderForm: {},
      createOrderForm: {},
      rules: {
        customerName: {
          required: true,
          message: "请输入客户名称",
          trigger: ["change", "blur"]
        },
        email: {
          type: "email",
          message: "请输入正确的邮箱地址",
          trigger: ["blur", "change"]
        }
      },
      groupData: [],
      csData: [],
      deleteId: 0,
      createId: 0,
      createCN: "",
      // tagColors: [
      //   "#f46363",
      //   "#2ed477"
      // ]
      fileList: []
    };
  },
  watch: {
    terminalValue(data) {
      // console.log(data);
    }
  },
  computed: {},
  methods: {
    cellStyle: function(row, column, rowIndex, columnIndex) {
      if (row.column.label === "访问状态") {
        return "color:#3cd681";
      } else {
        return "";
      }
    },
    handleClean() {
      this.$refs.createOrderForm.resetFields();
    },
    handleSizeChange(size) {
      this.pageSize = size;
      this.getPageData();
    },
    handleCurrentChange(page) {
      this.currentPage = page;
      this.getPageData();
    },
    getDetail(index, tableData) {
      this.$router.push({
        name: "customer-detail",
        params: tableData[index]
      });
    },
    createOrder(index, tableData) {
      this.addDialogVisible = true;
      this.createId = tableData[index].id;
      this.createCN = tableData[index].customerName;
    },
    deleteCustomer(index, tableData) {
      this.deleteDialogVisible = true;
      this.deleteId = tableData[index].id;
    },
    handlePartialDelete() {
      let params = this.multipleSelection.map(item => {
        return item.id;
      });
      params = params.join(",");
      api.partialDeleteCustomerAPI(params).then(res => {
        let { error, data } = res.data;
        if (error) {
          this.$message.error(data);
        } else {
          this.multipleSelection = [];
          this.getPageData();
          this.$message({
            message: "客户信息删除成功",
            type: "success"
          });
        }
      });
    },
    handleDelete() {
      this.deleteDialogVisible = false;
      api.deleteCustomerAPI(this.deleteId).then(res => {
        let { error, data } = res.data;
        if (error) {
          this.$message.error(data);
        } else {
          this.getPageData();
          this.$message({
            message: "删除客户成功",
            type: "success"
          });
        }
      });
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    getPageData() {
      this.isLoading = true;
      api
        .getAllCustomerAPI({
          page: this.currentPage,
          limit: this.pageSize
        })
        .then(res => {
          this.isLoading = false;
          let { error, data } = res.data;
          if (error) {
            this.$message.error("请求接口异常");
          } else {
            this.totalData = data.total;
            this.tableData = data.page;
          }
        });
    },
    handleCreateCustomer() {
      this.isLoading = true;
      this.createDialogVisible = false;
      this.$refs.createOrderForm.validate(valid => {
        if (valid) {
          api.createCustomerAPI(this.createOrderForm).then(res => {
            this.isLoading = false;
            let { error, data } = res.data;
            if (error) {
              this.$message.error(data);
            } else {
              this.getPageData();
              this.$message({
                message: "客户信息创建成功",
                type: "success"
              });
            }
          });
        } else {
          this.$message({
            message: "表单填写错误",
            type: "warning"
          });
        }
      });
    },
    handleCreateOrder() {
      this.$refs.addOrderForm.validate(valid => {
        if (valid) {
          this.addDialogVisible = false;
          api
            .createOrderAPI({
              workType: this.addOrderForm.workType,
              title: this.addOrderForm.title,
              attachments: this.addOrderForm.attachments,
              cclist: this.addOrderForm.cclist,
              priority: this.addOrderForm.priority,
              state: this.addOrderForm.state,
              customerServiceGroupId: this.addOrderForm.customerServiceGroupId,
              customerServiceId: this.addOrderForm.customerServiceId,
              customerId: this.addOrderForm.customerId,
              channel: "手动录入"
            })
            .then(res => {
              let { error, data } = res.data;
              if (error) {
                this.$message.error(data);
              } else {
                this.$message({
                  message: "工单创建成功",
                  type: "success"
                });
              }
            });
        } else {
          this.$message.error("工单填写有误");
        }
      });
    },
    addFile() {
      api.getFile(this.fileList).then(res => {
        let { error, data } = res.data;
        if (error) {
          this.$message.error(data);
        } else {
          this.$message({
            message: "附件上传成功",
            type: "success"
          });
        }
      });
    },
    handleSearch() {
      // this.isLoading = true;
      api
        .getCustomer2API({
          page: this.currentPage,
          limit: this.pageSize,
          level: this.serviceValue,
          channel: this.sourceValue
          // beginDate:this.value1,
          // endDate:this.value2
        })
        .then(res => {
          let { error, data } = res.data;
          if (error) {
            this.$message.error("请求接口异常");
          } else {
            this.totalData = data.total;
            this.tableData = data.page;
          }
        });
    }
  },
  created() {
    this.getPageData();
    api.getAllGroupAPI({}).then(res => {
      let { error, data } = res.data;
      if (error) {
        this.$message.error("请求接口异常");
      } else {
        this.groupData = data.page;
      }
    });
    api.getAllCustomerServiceAPI({}).then(res => {
      let { error, data } = res.data;
      if (error) {
        this.$message.error("请求接口异常");
      } else {
        this.csData = data.page;
      }
    });
    api.getCustomerAPI({});
  },
  mounted() {}
};
</script>

<style scoped>
.top-item-group {
  padding-bottom: 20px;
}
.visitor-container {
  margin: 20px;
  padding: 20px;
  background-color: #fff;
  border-radius: 5px;
}
.el-select {
  width: 162px;
}
.el-input {
  width: 200px;
}
.add-dialog-style {
  height: 440px;
  overflow: auto;
  padding: 20px 10px;
  margin-top: 10px;
  border-top: 1px solid #f2f2f2;
  border-bottom: 1px solid #f2f2f2;
}
.delete-dialog-style {
  border-top: 1px solid #f2f2f2;
  border-bottom: 1px solid #f2f2f2;
  padding: 20px;
  padding-bottom: 70px;
}
.middle-table-gorup {
  margin-top: 20px;
  width: 100%;
  display: inline-block;
}
.bottom-pagination-group {
  padding: 20px 20px 0;
  float: right;
}
</style>
